package edu.ua.bmr.android;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class LoginDialog extends Dialog implements OnClickListener, TextWatcher, View.OnKeyListener {

	private EditText mTxtUsername;
	private EditText mTxtPassword;
	private CheckBox mChkRememberPassword;
	private Button mBtnOk;
	private Button mBtnCancel;
	private LoginDialogListener mListener;
//	private Context mContext;
	
	public LoginDialog(Context context, LoginDialogListener listener) {
		super(context);
//		mContext = context;
		mListener = listener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_login);
		setTitle(R.string.please_login);

		mTxtUsername = (EditText) findViewById(R.id.txtUsername);
		mTxtPassword = (EditText) findViewById(R.id.txtPassword);
		mChkRememberPassword = (CheckBox) findViewById(R.id.chkRememberPassword);
		mBtnOk = (Button) findViewById(R.id.btnLoginOk);
		mBtnCancel = (Button) findViewById(R.id.btnLoginCancel);

		mTxtUsername.addTextChangedListener(this);
		mTxtPassword.addTextChangedListener(this);
		mTxtUsername.setOnKeyListener(this);
		mTxtPassword.setOnKeyListener(this);
		mBtnOk.setOnClickListener(this);
		mBtnCancel.setOnClickListener(this);
	}
	


	public boolean onKey(View v, int keyCode, KeyEvent event) {
		
		// Make the Enter button the same as clicking OK 
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
        	if ( mBtnOk.isEnabled() ) {
        		onClick(mBtnOk); // simulate button click
	        	return true;
        	}
        }
        return false;
	}

	   // Enable OK only when username & password have text
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		boolean enabled = getUsername().length() > 0 && getPassword().length() > 0;
		mBtnOk.setEnabled(enabled);
		mBtnOk.setFocusable(enabled);
	}
	
	public void afterTextChanged(Editable s) {
	}

	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	public String getUsername() {

		return mTxtUsername.getText().toString();
	}

	public String getPassword() {

		return mTxtPassword.getText().toString();
	}

	public boolean getRememberPassword() {

		return mChkRememberPassword.isChecked();
	}
	
	public void hideSoftKeyboard() {
		
		try {
			InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			if ( imm != null ) {
				// BUGFIX: using mBtnOk rather than getCurrentFocus() which sometimes returns null
				imm.hideSoftInputFromWindow(mBtnOk.getWindowToken(), 0);
			}
		}
		catch ( Exception ex ) {
			LoginActivity.logException( ex );
		}
	}

	public void setUser(String username) {

		clearFields();
		setTitle(R.string.please_login);

		if (username == null) {
			mTxtUsername.setEnabled(true);
			mTxtUsername.requestFocus();
		} else {
			mTxtUsername.setText(username);
			mTxtUsername.setEnabled(false);
			mTxtPassword.requestFocus();
		}
	}

	private void clearFields() {

		mTxtUsername.setText("");
		mTxtPassword.setText("");
		mChkRememberPassword.setChecked(false);
	}

	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btnLoginOk:
			if ( mListener.onOkClick(getUsername(), getPassword(), getRememberPassword()) ) {
//				clearFields();
				dismiss();
			}
			break;
		case R.id.btnLoginCancel:
			if ( mListener.onCancelClick() ) {
//				clearFields();
				cancel();
			}
			break;
		}
	}
}
